<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/14
 * Time: 16:50
 */

namespace Admin\Controller;


class ShopController extends AdminController
{
    public function _initialize(){
        parent::_initialize();
    }

    public function index(){
        $where=array();
        $uid=session('user_auth.uid');
        //审核权限
        if($uid!=1){
            $grout_id=M('auth_group_access')->where(array('uid'=>$uid))->find();
            if($grout_id['group_id']==10){
                $shop_info=M('member')->where(array('uid'=>$grout_id['uid']))->find();
                $where['id']=$shop_info['shop_id'];
            }
        }
        $listsCount = M('shop')->where($where)->count();
        $Page       = new \Think\Page($listsCount,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $lists = M('shop')->where($where)->order(' check_status asc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($lists as $ke=>$ve){
            $lists[$ke]['shop_area']=detail_area_name($ve['shop_area']);
        }
        $this->assign('list',$lists);
        $this->assign('group_id',$grout_id['group_id']);
        $this->assign('_page',$show);
        $this->display();
    }
    public function edit(){
        if(IS_POST){
            $dde = D('Shop')->create();
            if($dde){
                D('Shop')->where(array("id"=>$_POST['id']))->save($_POST);
                $this->success('修改成功',U('Shop/index'));
            }else{
                $this->error(D('Shop')->getError());
            }
        }else{
            $id=I('id');
            $list=M('Shop')->where(array('id'=>$id))->find();
            if($list){
                if($list['shop_thumb']){
                    $shop_thumb_id=explode(',',$list['shop_thumb']);
                    $shop_thumb=[];
                    foreach ($shop_thumb_id as $k=>$v){
                        $img_info=M('picture')->where(array('id'=>$v))->find();
                        $shop_thumb[$k]['url']="http://".$_SERVER['SERVER_NAME'].$img_info['path'];
                        $shop_thumb[$k]['id']=$v;
                    }
                    $list['shop_thumbs']=$shop_thumb;
                }
                if($list['shop_logo']){
                    $logo_info=M('picture')->where(array('id'=>$list['shop_logo']))->find();
                    $list['shop_logos']="http://".$_SERVER['SERVER_NAME'].$logo_info['path'];
                }
            }
            $this->assign('list',$list);
            $this->display();
        }
    }
    public function hq_shop($hq_shop=null,$id){
        if($id && $hq_shop){
            $status['hq_shop']=$hq_shop;
            $res=M('shop')->where(array('id'=>$id))->save($status);
            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $this->error('请选择商家');
        }
    }
}